package com.zhc.dejingapi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhc.dejingapi.entity.DataEntity;
import com.zhc.dejingapi.entity.query.SelectReportQuery;
import com.zhc.dejingapi.entity.vo.DataVo;
import com.zhc.dejingapi.entity.vo.QueVo;
import com.zhc.dejingapi.entity.vo.ResultVo;
import com.zhc.dejingapi.mapper.DataMapper;
import com.zhc.dejingapi.mapper.FormMapper;
import com.zhc.dejingapi.service.DataService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 所有申请的数据 服务实现类
 * </p>
 *
 * @author zhc
 * @since 2024-09-26
 */
@Service
public class DataServiceImpl extends ServiceImpl<DataMapper, DataEntity> implements DataService {

    @Resource
    private DataMapper dataMapper;

    @Resource
    private FormMapper formMapper;

    @Resource(name = "mySqlQFlowJdbcTemplate")
    private JdbcTemplate qflowJdbcTemplate;

    @Value("qflow.orderPool.appKey")
    private String orderPoolAppKey;

    @Value("qflow.workOrder.appKey")
    private String workOrderAppKey;

    @Override
    public String delData(Long applyId) {
        String que_title="下发数量";
        int delete=0;
        List<DataEntity> dataEntities = qflowJdbcTemplate.query("select * from data where value=?", new BeanPropertyRowMapper<>(DataEntity.class),applyId);
        for (DataEntity dataEntity : dataEntities) {
            //TODO 删除工序计划通过API
            int deleteProcessPlans = qflowJdbcTemplate.update("delete from data where apply_id=?", dataEntity.getApplyId());
            if(deleteProcessPlans==dataEntities.size()){
                QueVo que = qflowJdbcTemplate.queryForObject("SELECT que_id,que_title FROM form JOIN question ON form.form_id=question.form_id WHERE form.app_key=? AND que_title=?", QueVo.class, workOrderAppKey, que_title);
                DataVo dataVo = qflowJdbcTemplate.queryForObject("select que_id,apply_id,value FROM data WHERE que_id= ? AND apply_id =?", DataVo.class, que.getQueId(), applyId);
                if(dataVo!=null){

                }
                //TODO 删除工单通过API
                delete = qflowJdbcTemplate.update("delete from data where apply_id=?", applyId);
            }
        }
        if(delete>0){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }

    @Override
    public ResultVo selectReport(SelectReportQuery selectReportQuery) {
        Long num=0L;
        List<DataEntity> dataProcessPlans = qflowJdbcTemplate.query("select * from data where value=?", new BeanPropertyRowMapper<>(DataEntity.class),selectReportQuery.getDelApplyId());
        for (DataEntity dataProcessPlan : dataProcessPlans) {
            List<DataEntity> dataDispatch = qflowJdbcTemplate.query("select * from data where value=?", new BeanPropertyRowMapper<>(DataEntity.class),dataProcessPlan.getApplyId());
            for (DataEntity dispatch : dataDispatch) {
                Integer count = qflowJdbcTemplate.queryForObject("select COUNT(*) from data where value=?", Integer.class, dispatch.getApplyId());
                num+=count;
            }
        }
        ResultVo resultVo = new ResultVo();
        if(num>0){
            resultVo.setResult("否");
            resultVo.setMsg("存在报工无法删除");
            return resultVo;
        }else {
            resultVo.setResult("是");
            return resultVo;
        }

    }
}
